﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;


namespace SplittingFileInto1MBFiles
{
    class Program
    {
        static void SplitFile(string filename, int sizeInMBs, byte[] key , byte[] IV)
        {
            int sizeInBytes = sizeInMBs * 1024 * 1024;

            byte[] buffer = new byte[sizeInBytes];

            FileStream inputFile = File.Open(filename, FileMode.Open);
            using (inputFile)
            {
                int readBytes= inputFile.Read(buffer, 0, buffer.Length);
                int counter = 0;
                Rijndael RijndaelAlg = Rijndael.Create();

                    while (readBytes > 0)
                    {
                        counter++;
                        string dir =@"D:\CodeplexRepository\Panayot Popov\InputOutput\InputOutput\SplittingFileInto1MBFiles\bin\Debug\parted\"+counter.ToString() + "part_" + filename;
                        FileStream fs = File.Open(dir+ filename, FileMode.OpenOrCreate);
                        CryptoStream cStream = new CryptoStream(fs, RijndaelAlg.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                        BinaryWriter outputFile = new BinaryWriter(cStream);
                        using (outputFile)
                        {
                            using (fs)
                            {
                                using (cStream)
                                {
                                    outputFile.Write(buffer, 0, buffer.Length);
                                }
                            }
                        }
                        readBytes = inputFile.Read(buffer, 0, buffer.Length);
                    }
                
            }
        }

        static void UniteFiles(string dir, byte[] key , byte[] IV)
        {
            string[] files = Directory.GetFiles(dir);
            FileStream outputFile = File.Open("original.avi", FileMode.OpenOrCreate);
            using (outputFile)
            {
                foreach (var file in files)
                {
                    FileStream fStream = File.Open(file, FileMode.Open);
                    Rijndael RijndaelAlg = Rijndael.Create();

                    CryptoStream cStream = new CryptoStream(fStream,
                        RijndaelAlg.CreateDecryptor(key, IV),
                        CryptoStreamMode.Read);

                    BinaryReader bReader = new BinaryReader(cStream);
                    byte[] buffer = new byte[fStream.Length];
                    using (fStream)
                    {
                        using (cStream)
                        {
                            using (bReader)
                            {
                                bReader.Read(buffer, 0, buffer.Length);  
                                int i = 0;
                                Console.WriteLine(buffer[buffer.Length-1]);
                                while (buffer[i]!=0)
                                {
                                    outputFile.WriteByte(buffer[i]);
                                    i++;
                                }
                            }
                        }
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            Rijndael RijndaelAlg = Rijndael.Create();
            SplitFile("movie.avi", 1 , RijndaelAlg.Key , RijndaelAlg.IV);
            UniteFiles(@"D:\CodeplexRepository\Panayot Popov\InputOutput\InputOutput\SplittingFileInto1MBFiles\bin\Debug\parted", RijndaelAlg.Key, RijndaelAlg.IV);

        }
    }
}
